package com.atguigu.gulimall.order.web;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.exception.NoStockException;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.order.ao.OrderSubmitAo;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.ConfirmVo;
import com.atguigu.gulimall.order.vo.OderFareVo;
import com.atguigu.gulimall.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;



    /**
     * 下单功能
     * @param ao
     * @return
     */
    @PostMapping(value = "/submitOrder")
    public String submitOrder(OrderSubmitAo ao, Model model, RedirectAttributes attributes) {
        SubmitOrderResponseVo responseVo = orderService.submitOrder(ao);
        try {
            if (responseVo.getCode()==0){
                model.addAttribute("submitOrderResp",responseVo);
                return "pay";
            }else {
                String msg="下单失败";
                switch (responseVo.getCode()){
                    case 1: msg += "令牌订单信息过期，请刷新再次提交"; break;
                    case 2: msg += "订单商品价格发生变化，请确认后再次提交"; break;
                    case 3: msg += "库存锁定失败，商品库存不足"; break;
                }
                attributes.addAttribute("msg",msg);
                return "redirect:http://localhost:9060/toTrade";
            }
        }catch (Exception e){
            if (e instanceof NoStockException){
                String msg=((NoStockException)e).getMessage();
                attributes.addAttribute("msg",msg);
            }
            return "redirect:http://localhost:9060/toTrade";
        }
    }

        @RequestMapping("/toTrade")
    public String toTrade(Model model){
        ConfirmVo confirmVo=orderService.confirmOrder();
        model.addAttribute("confirmOrderData",confirmVo);
        return "confirm";
    }

    /**
     * 获取运费信息
     * @return
     */
    @GetMapping(value = "/fare")
    @ResponseBody
    public R getFare(@RequestParam("addrId") Long addrId) {
        OderFareVo fare = orderService.getFare(addrId);
     //   log.info("getFare.返回地址信息,fare={}", JSON.toJSONString(fare));
        return R.ok().setData(fare);
    }


    @GetMapping(value = "/memberOrder")
    public String memberOrder(@RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum) {
        Map<String,Object> page=new HashMap<>();
        page.put("page",pageNum);
        PageUtils pageUtils = orderService.queryPageWithItem(page);
        return "list.html";
    }

    @GetMapping(value = "/testMol")
    @ResponseBody
    public String memberOrder() {
        return "success";
    }



}
